<?php

class Application_Model_TagMapper {

    const ID     = 'tag_id';
    const TEXT   = 'text';
    const URL    = 'tag_url';
    const STATUS = 'status';

    protected static $_db;

    public function __consturct()
    {
        $this->_db = Zend_Registry::get('db_adaptor');
    }


    /**
     *
     * @param string $tag
     * @return Application
     *
     */
    public function lookupTag($tag)
    {
        $tag = trim($tag);
        $tag = trim($tag, '., '); //тепер точно...

        $tag = preg_replace('/\s+/', ' ', $tag);
        //$tag = preg_replace('/[^a-zA-Zа-яА-Я0-9-]+/u', '', $tag);

        $db = $this->_db;

        $q = "SELECT * FROM tag WHERE text=?";

        $res = $db->fetchAll($q, $tag);

        $resObj = new Application_Model_Tag();

        if (count($res) > 0) {
            $resObj->setId( $res[0]['tag_id'] );
            $resObj->setStatus( $res[0]['status'] );
            $resObj->setText( $res[0]['text'] );
            $resObj->setUrl( $res[0]['tag_url'] );
        } else {
            $resObj->setText($tag);
        }

        return $resObj;
    }

    /*************************************************************************/
    
    public function generateNewUrl($tag)
    {
        $tag = trim($tag, '., ');
        $tag = trim($tag);
        $tag = preg_replace('/\s+/', ' ', $tag);

        //@todo dor

        return $newTagUrl;

    }


}


?>
